package com.example.fruit.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.fruit.R;
import com.example.fruit.bean.Fruit;
import com.example.fruit.bean.Issue;
import com.example.fruit.bean.Orders;
import com.example.fruit.bean.User;
import com.example.fruit.util.SPUtils;
import com.example.fruit.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 订单详情信息
 */
public class FruitOrderDetailActivity extends AppCompatActivity {
    private Activity mActivity;
    private ImageView ivImg;
    private TextView tvTitle;
    private TextView tvEmail;
    private TextView tvIssuer;
    private TextView tvName;
    private TextView tvphone;
    private Button weifa,yifa,xg,tuikuan;
    private LinearLayout QQ;
    private ActionBar mActionBar;//标题栏
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_fruit_order_detail);
        ivImg = findViewById(R.id.img);
        tvTitle= findViewById(R.id.title);
        tvEmail = findViewById(R.id.email);
        tvIssuer = findViewById(R.id.issuer);
        tvName = findViewById(R.id.name);
        tvphone = findViewById(R.id.phone);
        QQ = findViewById(R.id.QQ);
        weifa=findViewById(R.id.weifa);
        yifa=findViewById(R.id.yifa);
        xg=findViewById(R.id.xg);
        tuikuan=findViewById(R.id.tuikuan);
        mActionBar = findViewById(R.id.myActionBar);
        //侧滑菜单
        mActionBar.setData(mActivity,"订单详情", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        String account = (String) SPUtils.get(mActivity,SPUtils.ACCOUNT,"");

        Orders orders2=DataSupport.where().findFirst(Orders.class);
       // Orders orders=(Orders) getIntent().getSerializableExtra("orders");
        // Orders orders = DataSupport.where("number=?",orders2.getNumber()).findFirst(Orders.class);
       // Issue issue = DataSupport.where().findFirst(Issue.class);
        Issue issue=DataSupport.where().findFirst(Issue.class);
        User user = DataSupport.where("account = ?", account).findFirst(User.class);
        Fruit fruit = (Fruit) getIntent().getSerializableExtra("fruit");
        tvTitle.setText(fruit.getTitle());
        tvEmail.setText(String.format("收货地址：       %s",user.getEmail()));
        tvName.setText(String.format("买家昵称：       %s",user.getNickName()));
        tvphone.setText(String.format("联系电话：       %s",user.getAge()));
        tvIssuer.setText(String.format("￥ %s",fruit.getIssuer()));
        Glide.with(mActivity)
                .asBitmap()
                .skipMemoryCache(true)
                .load(fruit.getImg())
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(ivImg);
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity,SPUtils.IS_ADMIN,false);
        QQ.setVisibility(isAdmin?View.GONE:View.VISIBLE);//管理员隐藏客服视图
        tvName.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        tvphone.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        tvEmail.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        xg.setVisibility(isAdmin?View.GONE:View.VISIBLE);
        tuikuan.setVisibility(isAdmin?View.VISIBLE:View.GONE);

        Issue issue5 = DataSupport.where("account = ? and title = ?",orders2.getAccount(), fruit.getTitle()).findFirst(Issue.class);
        weifa.setVisibility(issue5 != null ? View.GONE : View.VISIBLE);//收货表有这条数据就隐藏未发货按钮
        yifa.setVisibility(issue5 != null ? View.VISIBLE : View.GONE);

    }

    public void weifa(View view) {
        Orders orders = DataSupport.where().findFirst(Orders.class);
        User user = DataSupport.where("account = ? ", orders.getAccount()).findFirst(User.class);
        Fruit fruit = (Fruit) getIntent().getSerializableExtra("fruit");
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity, SPUtils.IS_ADMIN, false);
        String account = (String) SPUtils.get(mActivity,SPUtils.ACCOUNT,"");

        if (isAdmin) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.a1);//设置图标
            builder.setTitle("水果商城");//设置对话框的标题
            builder.setMessage("确定要将该商品发货吗？");//设置对话框的内容
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (isAdmin) {
                        Issue issue = new Issue(fruit.getTitle(),account);//获取总毫秒数 从1970年1月1日开始计算
                        issue.save();
                        Toast.makeText(mActivity, "订单状态已修改!", Toast.LENGTH_LONG).show();
                        weifa.setVisibility(View.GONE);
                        yifa.setVisibility(View.VISIBLE);
                    }
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }
            );
            AlertDialog a = builder.create();
            a.show();
        }else {
            Toast.makeText(mActivity, "该订单还未发货,请联系客服催促发货！", Toast.LENGTH_LONG).show();
        }
    }
    public void yifa(View view) {

        Toast.makeText(mActivity, "该订单已发货,请注意查收!", Toast.LENGTH_LONG).show();
        weifa.setVisibility(View.GONE);
    }
    public void tuikuan(View view) {
        Orders orders = DataSupport.where().findFirst(Orders.class);
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity, SPUtils.IS_ADMIN, false);
        if (isAdmin) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.a1);//设置图标
            builder.setTitle("水果商城");//设置对话框的标题
            builder.setMessage("确定要将该订单退款吗？");//设置对话框的内容
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (isAdmin) {
                        List<Orders> orders1 = DataSupport.where("title = ?",orders.getTitle()).find(Orders.class);
                        if (orders1 !=null && orders1.size() > 0){
                            for (Orders orders2 : orders1) {
                                orders2.delete();
                                finish();
                            }
                        }
                        Toast.makeText(mActivity, "退款成功!", Toast.LENGTH_LONG).show();
                        tuikuan.setVisibility(View.GONE);

                    }
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }
            );
            AlertDialog a = builder.create();
            a.show();
        }

    }
    public void QQ(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.a1);//设置图标
        builder.setTitle("水果商城");//设置对话框的标题
        builder.setMessage("确定要联系客服吗？");//设置对话框的内容
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //跳转到我的资料卡
                ProgressDialog dialog3 = new ProgressDialog(mActivity);
                dialog3.setMessage("正在连接客服···");
                dialog3.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(mActivity,"如无法联系，请添加好友后再试!", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqapi://card/show_pslcard?src_type=internal&source=sharecard&version=1&uin=1632957243")));//跳转到QQ资料
                        }catch (Exception e){
                            Toast.makeText(mActivity,"请安装最新版QQ后使用此功能!", Toast.LENGTH_LONG).show();
                        }
                        dialog3.dismiss();
                    }
                }, 1500);

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }
        );
        AlertDialog a=builder.create();
        a.show();  //必须show一下才能看到对话框
    }

    public void xg(View view){
        Boolean isAdmin = (Boolean) SPUtils.get(mActivity,SPUtils.IS_ADMIN,false);
         if (!isAdmin){

                Intent intent = new Intent(mActivity, PersonActivity.class);
                startActivity(intent);
                finish();

            }else {
            Intent intent = new Intent(mActivity, ManageActivity.class);
            startActivity(intent);
            finish();
        }
    }

}
